package com.frxuexi.park2305.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2305.R
import com.frxuexi.park2305.databinding.ActivityMyApplyBinding
import com.frxuexi.park2305.databinding.ActivityMyApproveBinding
import com.frxuexi.visit.adapter.StatusAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApproveActivity")
@AndroidEntryPoint
class MyApproveActivity : BaseActivity<ActivityMyApproveBinding, MyIntent, MyViewModel>() {
    override val binding: ActivityMyApproveBinding by lazy {
        ActivityMyApproveBinding.inflate(
            layoutInflater
        )
    }
    override val vm: MyViewModel by viewModels<MyViewModel>()
    val statusAdapter by lazy { StatusAdapter() }
    val myApproveAdapter by lazy { MyApproveAdapter() }
     var position=-1
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
                                val items = it.data as List<ParkBeanItem>
                                items.forEach { it.itemType = 0 }
                                myApproveAdapter.data.clear()
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()

                            }
                            RequestType.UPDATE->{
                                Log.i("lr","修改")
                                myApproveAdapter.notifyItemChanged(position)
                            }
                            RequestType.SELECT_VISIT->{
                                val items = it.data as List<VisitBeanItem>
                                items.forEach { it.itemType=1 }
                                myApproveAdapter.data.clear()
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()

                            }
                            RequestType.SELECT_NEWS->{
                                val items = it.data as List<NewsBeanItem>
                                val myitem=items.filter { it.published }
                                    myitem.forEach { it.itemType=2 }
                                myApproveAdapter.data.clear()
                                myApproveAdapter.addData(myitem)
                                myApproveAdapter.notifyDataSetChanged()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val types = mutableListOf<StatusBean>(
            StatusBean("全部", true),
            StatusBean("车位审核", false),
            StatusBean("来访审核", false),
            StatusBean("新闻审核", false)
        )
        statusAdapter.addData(types)
        binding.appRv.adapter = statusAdapter
        binding.appRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)

        statusAdapter.setOnItemClickListener { adapter, view, position ->
            statusAdapter.data.forEach {
                it.checked = false
            }
            statusAdapter.data.get(position).checked = true
            statusAdapter.notifyDataSetChanged()

            if(position==0){
                vm.sendIntent(MyIntent.getMyAppPark)
                vm.sendIntent(MyIntent.getNews)
                vm.sendIntent(MyIntent.getVisits)
            }else if(position==1){
                vm.sendIntent(MyIntent.getMyAppPark)
            }else if(position==2){
                vm.sendIntent(MyIntent.getVisits)
            }else{
                vm.sendIntent(MyIntent.getNews)
            }
        }

        binding.rvApprove.apply {
            adapter=myApproveAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity)
        }
        vm.sendIntent(MyIntent.getMyAppPark)
        vm.sendIntent(MyIntent.getNews)
        vm.sendIntent(MyIntent.getVisits)


        myApproveAdapter.addChildClickViewIds(com.frxuexi.park.R.id.btn_ok, com.frxuexi.park.R.id.btn_no,
            com.frxuexi.visit.R.id.vistit_ok, com.frxuexi.visit.R.id.visit_no)
        myApproveAdapter.setOnItemChildClickListener { adapter, view, position ->
            this.position=position
            val bean = myApproveAdapter.data.get(position)
            when(view.id){
                com.frxuexi.park.R.id.btn_ok->{
                    val beanItem = bean as ParkBeanItem
                    beanItem.spaceStatus="维修中"
                    vm.sendIntent(MyIntent.approvePark(beanItem.id!!,"维修中"))
                }
                com.frxuexi.park.R.id.btn_no->{
                    val beanItem = bean as ParkBeanItem
                    beanItem.spaceStatus="已占用"
                    vm.sendIntent(MyIntent.approvePark(beanItem.id!!,"已占用"))
                }
                com.frxuexi.visit.R.id.vistit_ok->{
                    val item = bean as VisitBeanItem
                    item.visitStatus="已通过"
                    vm.sendIntent(MyIntent.approveVisit(item.id!!))
                }
                com.frxuexi.visit.R.id.visit_no->{
                    val item = bean as VisitBeanItem
                    item.visitStatus="驳回"
                    vm.sendIntent(MyIntent.approveVisit(item.id!!))
                }
            }
        }
    }

}